//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    nav: [],
    nav_on: '0',
    banner: [],
    scrollHeight: '',
    nav_id: '',
    page: 1,
    h_page: 1,
    list: [],
    isAjax: true,
    scroll_top: 0,
  },
  onLoad: function () {
    let scrollHeight = wx.getSystemInfoSync().windowHeight;
    this.setData({
      scrollHeight: scrollHeight
    });

    this.homeData();
    //统计
    var mta = require('../../utils/mta_analysis.js');
    mta.Page.init();
  },
  homeData: function(id, page, type) {
  
    let that = this;

    if (that.data.isAjax) {
      wx.showLoading({
        title: '加载中...',
      })

      id = id || '';
      page = page || 1;
      type = type || '';
      wx.request({
        url: app.globalData.global_url + '/api/' + id,
        type: 'GET',
        data: { page: page },
        success: function (res) {
          // console.log(res.data.data.banner);
          if (res.data.code === 200) {
            if (type == '') 
            {
              let result = res.data.data;
              if (id == '') {
                if (result.banner.length === 0 && result.class.length === 0 && result.list.length === 0) {
                  wx.showToast({
                    title: '暂无更多数据',
                    icon: 'none',
                  });

                  that.setData({
                    isAjax: false
                  });
                } else {
                  that.setData({
                    nav: res.data.data.class,
                    banner: res.data.data.banner,
                    list: that.data.list.concat(res.data.data.list)
                  })
                }
              } else {
                if (res.data.data.data.length === 0) {
                  wx.showToast({
                    title: '暂无更多数据',
                    icon: 'none',
                  });

                  that.setData({
                    isAjax: false
                  });
                } else {
                  that.setData({
                    list: that.data.list.concat(res.data.data.data)
                  })
                }
              }
            }
            else
            {

              let is_list = id == "" ? res.data.data.list : res.data.data.data
              if (is_list.length < 10) {
                that.setData({
                  h_page: 1,
                  list: is_list,
                  isAjax: true
                })
              } else {
                that.setData({
                  list: is_list,
                })
              }
            }
          }
          wx.hideLoading();
        },
        error: function () {
          console.log(123)
          wx.hideLoading();
        }
      })
    }
  },
  navClick: function(e) {
    let that = this;
    let key = e.currentTarget.dataset.key || '0';
    let id = e.currentTarget.dataset.id || '';

    that.setData({ isAjax: true, list: [] })
    that.homeData(id, 1);

    that.setData({
      nav_on: key,
      nav_id: id,
      page: 1,
      scroll_top: 0,
    })
  },
  pageScrollToBottom: function() {
    let that = this;

    let page = that.data.page;

    page = page + 1;
    that.setData({ page: page });
      that.homeData(that.data.nav_id, page);
    // }
    
  },
  // 换一批
  switchAction: function() {
    let that = this;
    let h_page = that.data.list.length < 10 ? 1 : that.data.h_page + 1;
    that.setData({
      h_page: h_page,
      isAjax: true,
      page: h_page,
      scroll_top: 0,
    })

    that.homeData(that.data.nav_id, h_page, 1);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let that = this;
    let id = res.target.dataset.id;
    let title = res.target.dataset.title;
    let img = res.target.dataset.img;
    
    return {
      title: title,
      path: '/pages/info/info?id=' + id,
      imageUrl: img,
      success: (res) => {
        wx.showToast({
          title: '转发成功',
        });

      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})
